package com.wuzuqing.android.eightdiagrams.task;

import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaItem;
import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaModel;
import com.wuzuqing.android.eightdiagrams.utils.DataUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 26.7.24 3:30 PM
 * @Description: 请写描述
 */
public class QiMenDunJiaStep4Task extends IQiMenDunJiaTask{
    QiMenDunJiaModel model;

    public QiMenDunJiaStep4Task(QiMenDunJiaModel model) {
        this.model = model;
    }

    @Override
    public void handleTask() {

        Map<String, String> liuYiMap = new HashMap<>();
        liuYiMap.put("甲子", "戊");
        liuYiMap.put("甲戌", "己");
        liuYiMap.put("甲申", "庚");
        liuYiMap.put("甲午", "辛");
        liuYiMap.put("甲辰", "壬");
        liuYiMap.put("甲寅", "癸");

        List<QiMenDunJiaItem> list = model.getList();

        List<String> baZi = model.getLunar().getBaZi();
        String xunShou = DataUtils.getXunShou(baZi.get(3));

        //看时柱
        //值班的地支宫位
        String zhiGan = liuYiMap.get(xunShou) + "";
        model.setXunShou(xunShou);
        model.setZhiGan(zhiGan);

        //值班的地支宫位
        String zhiMen = "";
        String zhiXin = "";
        int zhiGongNum = 0;
        int menGongShu = 0;
        //找原始值班的九星和八门
        List<String> ZhiList = Arrays.asList("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥");
        String zhiZhi = xunShou.charAt(1)+"";
        String shiZhi = baZi.get(3).charAt(1)+"";
        int zhiZhiIndex = ZhiList.indexOf(zhiZhi);
        int shiZhiIndex = ZhiList.indexOf(shiZhi);
        for (int i = 0; i < list.size(); i++) {
            QiMenDunJiaItem dunJia = list.get(i);
            if (dunJia.getDiPanGan().startsWith(zhiGan)) {
                zhiMen = dunJia.getOriginMen();
                zhiXin = dunJia.getOriginXing();
                zhiGongNum = dunJia.getGongNum();
                model.setOriginGongNum(zhiGongNum);
                if (shiZhiIndex==zhiZhiIndex){
                    menGongShu = zhiGongNum;
                }else{
                    int step = 0;
                    for (int k = 0; k < 12; k++) {
                        if (shiZhiIndex == (zhiZhiIndex + k) % 12) {
                            break;
                        }
                        ++step;
                    }
                    if (model.isYangDun()){
                        menGongShu = zhiGongNum + step;
                    }else{
                        menGongShu = zhiGongNum - step + 9;
                    }
                }
                menGongShu %= 9;
                if (menGongShu == 0) {
                    menGongShu = 9;
                }
                if (dunJia.getGongNum() == 5) {
                    QiMenDunJiaItem temp = getDunJia(list, 2);
                    zhiMen = temp.getOriginMen();
                    zhiGongNum = 2;
                }
                break;
            }
        }
        String shiGan = baZi.get(3).charAt(0)+"";
        for (int i = 0; i < list.size(); i++) {
            QiMenDunJiaItem dunJia = list.get(i);
            if (dunJia.getDiPanGan().startsWith(shiGan)) {
                zhiGongNum = dunJia.getGongNum();
                break;
            }
        }
        model.setZhiMen(zhiMen);
        model.setZhiXin(zhiXin);
        model.setMenGongShu(menGongShu);
        model.setZhiGongNum(zhiGongNum);

//        System.out.println("第四,五,六步:旬首" + xunShou + "旬"
//                + ",zhiGan=" + zhiGan + ",zhiXin="+zhiXin+zhiGongNum+",zhiMen="+zhiMen
//                +menGongShu
//                +",shiShu="
//                +model.getShiShu());
    }
}
